package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscNetInfoResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemDeploy;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscSystemDeployDto;
import com.glsc.ngateway.platform.service.glres.GlscNetInfoResourceService;
import com.glsc.ngateway.platform.service.glres.GlscSystemDeployService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 资源管理-系统部署情况
 */
@RestController
@RequestMapping("/api/res/system/deploy")
@Slf4j
public class GlscSystemDeployController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscSystemDeployService glscSystemDeployService;

    @Resource
    private GlscNetInfoResourceService netInfoResourceService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscSystemDeploy>> page(@RequestBody GlscSystemDeployDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getDeployNodeName())) {
            param.put("LIKE_nodeName", dto.getDeployNodeName());
        }

        if (Objects.isNull(dto.getSystemId())) {
            PlatformResponse.successData(Page.empty());
        }
        param.put("EQ_systemId", dto.getSystemId());

        param.put("EQ_sourceType", dto.getSourceType());

        Page<GlscSystemDeploy> page = glscSystemDeployService.page(dto.getPageNo(), dto.getPageSize(), param);
        List<GlscSystemDeploy> deployList = page.getContent();
        if (CollectionUtils.isNotEmpty(deployList)) {
            for (GlscSystemDeploy deploy : deployList) {
                GlscNetInfoResource netInfo = netInfoResourceService.findByIp(deploy.getNodeIp());
                if (Objects.nonNull(netInfo)) {
                    deploy.setNetInfoId(netInfo.getId());
                }
            }
        }

        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<GlscSystemDeploy> findById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(glscSystemDeployService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody GlscSystemDeploy obj) {
        glscSystemDeployService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据地删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody GlscSystemDeploy obj) {
        glscSystemDeployService.delete(obj.getId());
        return PlatformResponse.success();
    }


}
